//
//  YZChangePasswordViewController.m
//  kuaiyidemo1
//
//  Created by 刘邓诏初 on 16/5/5.
//  Copyright © 2016年 刘邓诏初. All rights reserved.
//

#import "YZChangePasswordViewController.h"
#import "YZPasswordView.h"
#import "SVProgressHUD.h"
/// 控件间距
#define viewMargin 70
#define viewWidth 220

@interface YZChangePasswordViewController ()
/// 原密码输入view
@property(nonatomic,strong)YZPasswordView *oldPasswordTextView;
/// 新密码输入view
@property(nonatomic,strong)YZPasswordView *PasswordTextView;
/// 新密码确认view
@property(nonatomic,strong)YZPasswordView *confirmPasswordTextView;
/// 完成按钮
@property(nonatomic,strong)UIButton *accomplishButton;
@end

@implementation YZChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    [self prepareUI];
    [self.oldPasswordTextView.passwordTextField becomeFirstResponder];
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)prepareUI{
    // 添加子控件
    [self.view addSubview:self.oldPasswordTextView];
    [self.view addSubview:self.PasswordTextView];
    [self.view addSubview:self.confirmPasswordTextView];
    [self.view addSubview:self.accomplishButton];
    
    // 禁用 autoresizing
    self.oldPasswordTextView.translatesAutoresizingMaskIntoConstraints = NO;
    self.PasswordTextView.translatesAutoresizingMaskIntoConstraints = NO;
    self.confirmPasswordTextView.translatesAutoresizingMaskIntoConstraints = NO;
    self.accomplishButton.translatesAutoresizingMaskIntoConstraints = NO;
    
    // 设置约束
    // 原密码输入框
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.oldPasswordTextView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:100]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.oldPasswordTextView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.oldPasswordTextView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:45]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.oldPasswordTextView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:viewWidth]];
    
    // 新密码输入框
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.PasswordTextView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.oldPasswordTextView attribute:NSLayoutAttributeTop multiplier:1 constant:viewMargin]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.PasswordTextView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.oldPasswordTextView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.PasswordTextView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:45]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.PasswordTextView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:viewWidth]];
    
    // 新密码确认框
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.confirmPasswordTextView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.PasswordTextView attribute:NSLayoutAttributeTop multiplier:1 constant:viewMargin]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.confirmPasswordTextView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.PasswordTextView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.confirmPasswordTextView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:45]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.confirmPasswordTextView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:viewWidth]];
    
    // 完成按钮
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.accomplishButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.confirmPasswordTextView attribute:NSLayoutAttributeTop multiplier:1 constant:viewMargin]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.accomplishButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.confirmPasswordTextView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.accomplishButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:45]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.accomplishButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:200]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - 懒加载
- (YZPasswordView *)oldPasswordTextView{
    if (!_oldPasswordTextView) {
        _oldPasswordTextView = [YZPasswordView passwordView];
        _oldPasswordTextView.passwordTextField.placeholder = @"请输入原密码";
    }
    return _oldPasswordTextView;
}

- (YZPasswordView *)PasswordTextView{
    if (!_PasswordTextView) {
        _PasswordTextView = [YZPasswordView passwordView];
        _PasswordTextView.passwordTextField.placeholder = @"请输入新登录密码";
    }
    return _PasswordTextView;
}

- (YZPasswordView *)confirmPasswordTextView{
    if (!_confirmPasswordTextView) {
        _confirmPasswordTextView = [YZPasswordView passwordView];
        _confirmPasswordTextView.passwordTextField.placeholder = @"请再次输入新密码";
    }
    return _confirmPasswordTextView;
}

- (UIButton *)accomplishButton{
    if (!_accomplishButton) {
        _accomplishButton = [[UIButton alloc] init];
        [_accomplishButton setTitle:@"完成" forState:UIControlStateNormal];
        [_accomplishButton setBackgroundImage:[UIImage imageNamed:@"buttonBackground"] forState:UIControlStateNormal];
        [_accomplishButton addTarget:self action:@selector(didClickAccomplishButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _accomplishButton;
}

#pragma mark - 按钮点击事件
- (void)didClickAccomplishButton{

    if (self.oldPasswordTextView.passwordTextField.text.length > 0 && self.PasswordTextView.passwordTextField.text.length > 0 && self.confirmPasswordTextView.passwordTextField.text.length > 0 && [self.oldPasswordTextView.passwordTextField.text isEqualToString:@"11"] && [self.PasswordTextView.passwordTextField.text isEqualToString:self.confirmPasswordTextView.passwordTextField.text]) {
        [SVProgressHUD showSuccessWithStatus:@"修改密码成功"];
    }
    else {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的密码。可能你的密码为空或者两次输入的新密码不一致"];
    }
    self.oldPasswordTextView.passwordTextField.text = nil;
    self.PasswordTextView.passwordTextField.text = nil;
    self.confirmPasswordTextView.passwordTextField.text = nil;
    [self.view endEditing:YES];
}

@end
